package com.hitgo.soccer.database;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

public class BaseDAO 
{
	public static void commitTransaction(Object nUserId) throws SQLException 
	{
		SqlMapClient sqlMapper = getSQLMapper(nUserId);
		commitTransaction(sqlMapper);
	}
	
	public static void endTransaction(long nUserId) throws SQLException 
	{
		SqlMapClient sqlMapper = getSQLMapper(nUserId);
		endTransaction(sqlMapper);
	}
	
	public static void startTransaction(SqlMapClient sqlMapper) throws SQLException 
	{
		sqlMapper.startTransaction();
	}
	
	public static void commitTransaction(SqlMapClient sqlMapper) throws SQLException 
	{
		sqlMapper.commitTransaction();
	}
	
	public static void endTransaction(SqlMapClient sqlMapper) throws SQLException 
	{
		sqlMapper.endTransaction();
	}
	
	public static SqlMapClient getSQLMapper(Object userID) 
	{
		SqlMapClient spc = null;
		if(userID instanceof String)
			spc = DBManager.GetSQLMapper((String)userID);
		else
			spc = DBManager.GetSQLMapper((Long)userID);
		return spc;
	}
	
	
	public static List queryForList(Object userID, String method, Object param) throws SQLException
	{
		SqlMapClient spc = null;
		if(userID instanceof String)
			spc = getSQLMapper((String)userID);
		else
			spc = getSQLMapper((Long)userID);
		
		List lResult = null;
		if(param == null)
		{
			lResult = spc.queryForList(method);
		}
		else
		{
			lResult = spc.queryForList(method, param);
		}
		return lResult;
	}
	
	public static Object queryForObject(Object lUserID, String method) throws SQLException
	{
		return queryForObject(lUserID, method, null);
	}
	
	public static Object queryForObject(Object lUserID, String method, Object param) throws SQLException
	{
		SqlMapClient spc = getSQLMapper(lUserID);
		Object lResult = null;
		if(param == null)
		{
			lResult = spc.queryForObject(method);
		}
		else
		{
			lResult = spc.queryForObject(method, param);
		}
		return lResult;
	}
}
